//
//  ProgressEnquiry.m
//  homeland
//
//  Created by SunShine on 13-11-2.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "ProgressEnquiryService.h"
#import "UserInfoService.h"

@implementation ProgressEnquiryService

-(void)requestEnterprisePeogressWithKey:(NSString *)key
{
    _currentPageNo = 0;
    if (self.dataList) {
        self.dataList = nil;
    }
    [self requestMoreEnterprisePeogressWithKey:key];
}
-(void)requestMoreEnterprisePeogressWithKey:(NSString *)key
{
    _dataItemName = @"EnterpriseProgressListItem";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (key) {
//        [params setValue:key forKey:@"user_id"];
        [params setValue:key forKey:@"itemCode"];
    }
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if (userInfo.currentLoginUserName && userInfo.currentLoginUserName.length>0) {
        [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    [self doHttpPost:ENTERPRISE_PROGRESS_LIST_URL headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

-(void)requestResidentPeogressWithName:(NSString *)name ID:(NSString *)ID;
{
    _currentPageNo = 0;
    if (self.dataList) {
        self.dataList = nil;
    }
    [self requestMoreResidentPeogressWithName:name ID:ID];
}
-(void)requestMoreResidentPeogressWithName:(NSString *)name ID:(NSString *)ID;
{
    _dataItemName = @"ResidentProgressListItem";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    [params setValue:name forKey:@"sqrmc"];
    [params setValue:ID forKey:@"sblbh"];

    [self doHttpPost:RESIDENT_PROGRESS_LIST_URL headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

-(void)requestEnterpriseProgressDetailForKey:(NSString *)key
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:key forKey:@"control_seq"];
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName: ENTERPRISE_PROGRESS_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:nil params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        //转换对象,添加到列表中
        _enterpriseDetailItem = getObjectFromDic([EnterpriseProgressDetailItem class], json);
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}
-(void)feedbackEnterpriseProgressWithID:(NSString *)Id AndVote:(NSInteger)vote
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:Id forKey:@"id"];
    [params setValue:[NSString stringWithFormat:@"%d",vote] forKey:@"vote_grade"];
    [self votePost:ENTERPROSE_PROGRESS_VOTE_URL headers:nil path:nil parameters:params];
}

-(void)requestResidentProgressDetailForKey:(NSString *)key
{
    _currentEngine = [[MKNetworkEngine alloc] initWithHostName: RESIDENT_PROGRESS_DETAIL_URL customHeaderFields:nil];
    MKNetworkOperation *op = [_currentEngine operationWithPath:key params:nil httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        NSDictionary *json = [operation responseJSON];
        //转换对象,添加到列表中
        _residentDetailItem = getObjectFromDic([ResidentProgressDetailItem class], json);
        SuccessBlock(json);
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        //执行回调
        FailBlock(err);
    }];
    [_currentEngine enqueueOperation:op];
}
-(void)feedbackResidentProgressWithID:(NSString *)Id Code:(NSString *)code AndVote:(NSInteger)vote
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:Id forKey:@"beVotedId"];
    [params setValue:code forKey:@"beVotedCode"];
    [params setValue:[NSString stringWithFormat:@"%d",vote] forKey:@"vote_grade"];
    if ([UserInfoService getUserInfoService].currentLoginUserName) {
        [params setValue:[UserInfoService getUserInfoService].currentLoginUserName forKey:@"voteUserName"];
        [params setValue:[UserInfoService getUserInfoService].userInfo.Id forKey:@"voteUserId"];
    } else {
        [params setValue:@"匿名" forKey:@"voteUserName"];
        [params setValue:@"" forKey:@"voteUserId"];
    }
    [self votePost:RESIDENT_PROGRESS_VOTE_URL headers:nil path:nil parameters:params];
}

-(void)votePost:(NSString *)posturl headers:(NSDictionary*)headersOrNil path:(NSString*)path parameters:(NSDictionary *)params
{
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:posturl customHeaderFields:headersOrNil];
    MKNetworkOperation *op = [currentEngine operationWithPath:path params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *operation) {
        
        BOOL isSuccess = NO;
        if ([[operation responseString] isEqualToString:@"true"]) {
            isSuccess = YES;
        } else {
            NSDictionary *json = [operation responseJSON];
//            self.dealCode = [json objectForKey:@"dealCode"];
            isSuccess = ((NSNumber*)[json objectForKey:@"isSuccess"]).boolValue;
        }
        //执行回调
        if (isSuccess) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
                [self.delegate loadDataFinished];
            }
        } else {
            if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
                [self.delegate loadDataFaild:@"提交失败"];
            }
        }
        
    }errorHandler:^(MKNetworkOperation *errorOp, NSError* err) {
        NSLog(@"MKNetwork request error : %@", [err localizedDescription]);
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFaild:)]) {
            [self.delegate loadDataFaild:@"服务器网络异常"];
        }
    }];
    [currentEngine enqueueOperation:op];
}
@end
